//
//  AllView.m
//  HWUI06_UIScrollView嵌套，相册
//
//  Created by 崔将军 on 16/3/19.
//  Copyright © 2016年 崔将军. All rights reserved.
//

#import "AllView.h"
#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height

@implementation AllView

{
    NSInteger _picNum;
}

- (instancetype)initWithFrame:(CGRect)frame images:(NSArray *)images {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createView];
        [self createScroll:images];
        
    }
    return self;
}

- (void)createScroll:(NSArray *)images {
    
    _picNum = images.count;
    self.bigScrollView.contentSize = CGSizeMake((images.count + 2) * WIDTH, 0);
    
    // smallScrollView
    for (NSInteger i = 0; i < images.count; i ++) {
        
        SmallScrollView *smallScroll = [[SmallScrollView alloc] initWithFrame:CGRectMake(WIDTH * (i + 1), 0, WIDTH, HEIGHT) images:images[i]];
        
        [self.bigScrollView addSubview:smallScroll];
        [smallScroll release];
    }
    
    // firstScroll
    SmallScrollView *firstScroll = [[SmallScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) images:images.lastObject];
    [self.bigScrollView addSubview:firstScroll];
    [firstScroll release];
    
    // lastScroll
    SmallScrollView *lastScroll = [[SmallScrollView alloc] initWithFrame:CGRectMake(WIDTH * 8, 0, WIDTH, HEIGHT) images:images.firstObject];
    [self.bigScrollView addSubview:lastScroll];
    [lastScroll release];
    
    // 添加页数
    self.page.numberOfPages = images.count;
}

- (void)createView {
    
    self.bigScrollView  = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    
    self.bigScrollView.pagingEnabled = YES;
    self.bigScrollView.showsHorizontalScrollIndicator = NO;
    
    self.bigScrollView.delegate = self;
    self.bigScrollView.contentOffset = CGPointMake(WIDTH, 0);
    [self addSubview:self.bigScrollView];
    [_bigScrollView release];
    
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(100, HEIGHT - 50, 200, 50)];
    [self addSubview:self.page];
    self.page.currentPageIndicatorTintColor = [UIColor redColor];
    self.page.pageIndicatorTintColor = [UIColor blackColor];
    self.page.backgroundColor = [UIColor cyanColor];
    [_page release];
    
    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
}

- (void)pageAction:(UIPageControl *)page {
    
    [self.bigScrollView setContentOffset:CGPointMake((page.currentPage + 1) * WIDTH, 0) animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if (scrollView.contentOffset.x == (_picNum + 1) * WIDTH) {
        scrollView.contentOffset = CGPointMake(WIDTH, 0);
    } else if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(WIDTH * (_picNum), 0);
    }
    self.page.currentPage = scrollView.contentOffset.x / WIDTH - 1;
    
    // 完成缩放
    // 先遍历
    for (UIView *view in self.bigScrollView.subviews) {
        
        if ([view isKindOfClass:[SmallScrollView class]]) {
            
            SmallScrollView *scroll = (SmallScrollView *)view;
            scroll.zoomScale = 1.0;
        }
    }
    
}


@end
